package com.yang.mall.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yang.common.utils.R;
import com.yang.mall.product.model.vo.CategoryTreeNodeVO;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yang.common.utils.PageUtils;
import com.yang.common.utils.Query;

import com.yang.mall.product.dao.CategoryDao;
import com.yang.mall.product.entity.CategoryEntity;
import com.yang.mall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryTreeNodeVO> listWithTree() {
        //  1.获取所有分类
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
        if (CollectionUtil.isEmpty(categoryEntities)) {
            return new ArrayList<>();
        }
        //  2.将这些节点全部都存放到map中
        HashMap<Long,CategoryTreeNodeVO>nodeMap = new HashMap<>();
        for (CategoryEntity categoryEntity : categoryEntities) {
            CategoryTreeNodeVO node = BeanUtil.copyProperties(
                    categoryEntity, CategoryTreeNodeVO.class
            );
            nodeMap.put(categoryEntity.getCatId(),node);
        }
        //  3.遍历分类，进行树形构建
        List<CategoryTreeNodeVO>treeList = nodeMap.values()
                .stream()
                .filter(c->c.getParentCid() == 0)
                .collect(Collectors.toList());
        for (Long catId : nodeMap.keySet()) {
            CategoryTreeNodeVO node = nodeMap.get(catId);
            CategoryTreeNodeVO parent = nodeMap.get(node.getParentCid());
            if (parent != null) {
                if (parent.getChildren() == null) {
                    parent.setChildren(new ArrayList<>());
                }
                parent.getChildren().add(node);
            }
        }
        //  4.排序
        for (CategoryTreeNodeVO nodeVO : treeList) {
            categoryTreeSort(nodeVO);
        }
        return treeList;
    }
    private void categoryTreeSort(CategoryTreeNodeVO root) {
        if (root == null){
            return;
        }
        if (CollectionUtil.isEmpty(root.getChildren())) {
            return;
        }
        Collections.sort(root.getChildren(),(a,b) -> {
            if (a.getSort() == null && b.getSort() == null){
                return 0;
            }
            if (a.getSort() == null){
                return 1;
            }
            if (b.getSort() == null){
                return -1;
            }
            return a.getSort() - b.getSort();
        });
        for (CategoryTreeNodeVO child : root.getChildren()) {
            categoryTreeSort(child);
        }
    }



}